/**
 * 
 */
package it.unitn.testbed.server.task;

import gwtupload.server.UploadAction;
import gwtupload.server.exceptions.UploadActionException;
import it.unitn.testbed.server.serial.SerialReceiver;

import java.io.File;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.FileItem;
import org.apache.log4j.Logger;

/**
 * Handles the upload of serial messages classes to be used by
 * {@link SerialReceiver}.
 * 
 * @author Stefan Guna
 * @see SerialReceiver
 * 
 */
public class SerialMsgUpload extends UploadAction {
	public static Logger log = Logger
			.getLogger(SerialMsgUpload.class.getName());

	private static final long serialVersionUID = 1L;

	/*
	 * (non-Javadoc)
	 * 
	 * @seegwtupload.server.UploadAction#executeAction(javax.servlet.http.
	 * HttpServletRequest, java.util.List)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public String executeAction(HttpServletRequest request,
			List<FileItem> sessionFiles) throws UploadActionException {
		HttpSession session = request.getSession(true);
		HashMap<String, File> serialMsgs;

		synchronized (session) {
			serialMsgs = (HashMap<String, File>) session
					.getAttribute("serialMsgs");
			if (serialMsgs == null) {
				serialMsgs = new HashMap<String, File>();
				session.setAttribute("serialMsgs", serialMsgs);
			}
		}

		for (FileItem item : sessionFiles) {
			if (item.isFormField())
				continue;
			File file;
			try {
				file = File.createTempFile("serial-", null, new File("/tmp"));
			} catch (Exception e) {
				log.error(e.getMessage());
				for (StackTraceElement ste : e.getStackTrace())
					log.error(ste);
				return "Cannot create temporary file.";
			}
			try {
				item.write(file);
			} catch (Exception e) {
				file.delete();
				log.error(e.getMessage());
				for (StackTraceElement ste : e.getStackTrace())
					log.error(ste);
				return "Cannot dump contents of the file.";
			}
			synchronized (session) {
				serialMsgs.put(item.getFieldName(), file);
				log.trace(file + " containing serial message saved");
			}
		}
		return "File successfully uploaded.";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seegwtupload.server.UploadAction#removeItem(javax.servlet.http.
	 * HttpServletRequest, java.lang.String)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void removeItem(HttpServletRequest request, String fieldName)
			throws UploadActionException {
		HttpSession session = request.getSession(true);
		HashMap<String, File> serialMsgs;
		HashMap<String, String> serialMsgNames;
		File file;

		synchronized (session) {
			serialMsgs = (HashMap<String, File>) session
					.getAttribute("serialMsgs");
			serialMsgNames = (HashMap<String, String>) session
					.getAttribute("serialMsgNames");
			if (serialMsgs == null)
				return;
			file = serialMsgs.get(fieldName);
			serialMsgs.remove(fieldName);
			serialMsgNames.remove(fieldName);
		}
		if (file != null)
			file.delete();
	}
}
